using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;

namespace Core.Extension.UIAnimation
{
    /// <summary>
    /// UI动画组件,基于DoTween
    /// </summary>
    public class UIAnimation : MonoBehaviour
    {
        public bool playOnAwake = false;
        public UIAnimationType animType = UIAnimationType.Scale;
        [Header("出现动画的参数")] public UIAnimationData showScaleAnim;

        [Header("出现动画的参数")] public UIAnimationData showMoveAnim;

        [Header("隐藏动画的参数")] public UIAnimationData hideScaleAnim;

        [Header("隐藏动画的参数")] public UIAnimationData hideMoveAnim;

        public Transform animTarget;

        private CanvasGroup _canvasGroup;

        public void Awake()
        {
            //根据自身的喜好可自行修改
            if (animTarget == null) animTarget = GetComponent<Transform>();
            _canvasGroup = animTarget.GetComponent<CanvasGroup>();
            if (_canvasGroup == null)
                _canvasGroup = animTarget.gameObject.AddComponent<CanvasGroup>();
            if (playOnAwake)
                Show();
        }

        /// 展示
        public virtual List<Tweener> Show(UnityAction showComplete = null)
        {
            List<Tweener> allTweener = new List<Tweener>();
            if (animType == UIAnimationType.Scale)
            {
                //如果缩放动画需要音效
                var tweener = AnimationFunction.WindowScale(animTarget, showScaleAnim, showComplete);
                var tweenerFade = AnimationFunction.Fade(_canvasGroup, showScaleAnim);
                allTweener.Add(tweener);
                allTweener.Add(tweenerFade);
            }
            else if (animType == UIAnimationType.Move)
            {
                var tweener = AnimationFunction.WindowMove(animTarget, showMoveAnim, showComplete);
                var tweenerFade = AnimationFunction.Fade(_canvasGroup, showMoveAnim);
                allTweener.Add(tweenerFade);
                allTweener.Add(tweener);
            }

            return allTweener;
        }

        /// 隐藏
        public virtual List<Tweener> Hide(UnityAction hideComplete = null)
        {
            List<Tweener> allTweener = new List<Tweener>();
            if (animType == UIAnimationType.Scale)
            {
                var tweener = AnimationFunction.WindowScale(animTarget, hideScaleAnim, hideComplete);
                var tweenerFade = AnimationFunction.Fade(_canvasGroup, hideScaleAnim);
                allTweener.Add(tweener);
                allTweener.Add(tweenerFade);
            }
            else if (animType == UIAnimationType.Move)
            {
                var tweener = AnimationFunction.WindowMove(animTarget, hideMoveAnim, hideComplete);
                var tweenerFade = AnimationFunction.Fade(_canvasGroup, hideMoveAnim);
                allTweener.Add(tweener);
                allTweener.Add(tweenerFade);
            }

            return allTweener;
        }
    }

    /// <summary>
    /// 设置UI动画时的参数
    /// </summary>
    [System.Serializable]
    public class UIAnimationData
    {
        public Vector3 start;
        public Vector3 end;
        public int isLoop = 1;
        public float timer = 1;
        public float delay;
        public float startAlpha = 1;
        public float endAlpha = 1;
        public Ease easeType = Ease.OutCubic;
        public LoopType loopType = LoopType.Yoyo;
    }
}